/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.texture;

import com.android1.amarena2d.commons.IntRectangle;
import com.android1.amarena2d.engine.Resource;

public class TextureFrameImpl implements TextureFrame {

    private final IntRectangle rectangle = new IntRectangle();
    private final ManagedTexture managedTexture;
    private TexturedMesh sharedTexturedMesh;
    private String tag;


    public TextureFrameImpl(final ManagedTexture managedTexture) {
        this.managedTexture = managedTexture;
        //todo rework
        if (managedTexture.getTextureFrame() != this) {
            if (managedTexture.isInit()) {
                this.rectangle.setHeight(managedTexture.getWidth());
                this.rectangle.setWidth(managedTexture.getHeight());
            } else {
                this.managedTexture.register(new Resource.StateListener() {
                    @Override
                    public void onInit() {
                        rectangle.set(managedTexture.getTextureFrame().getRectangle());
                    }

                    @Override
                    public void onDispose() {

                    }
                });
            }
        }
    }

    public TextureFrameImpl(final ManagedTexture managedTexture, final IntRectangle rectangle) {
        this(managedTexture, rectangle.getX(), rectangle.getY(), rectangle.getWidth(), rectangle.getHeight());
    }

    public TextureFrameImpl(final ManagedTexture managedTexture, final int x, final int y, final int width, final int height) {
        this.managedTexture = managedTexture;
        this.rectangle.setX(x);
        this.rectangle.setY(y);
        this.rectangle.setHeight(height);
        this.rectangle.setWidth(width);
    }

    @Override
    public int getX() {
        return rectangle.getX();
    }

    @Override
    public void setX(int x) {
        rectangle.setX(x);

    }

    @Override
    public int getY() {
        return rectangle.getY();
    }

    @Override
    public void setY(int y) {
        rectangle.setY(y);

    }

    @Override
    public int getWidth() {
        return rectangle.getWidth();
    }

    @Override
    public void setWidth(int width) {
        rectangle.setWidth(width);
    }

    @Override
    public int getHeight() {
        return rectangle.getHeight();
    }

    @Override
    public void setHeight(int height) {
        rectangle.setHeight(height);
    }

    @Override
    public void set(int x, int y, int width, int height) {
        this.rectangle.setX(x);
        this.rectangle.setY(y);
        this.rectangle.setHeight(height);
        this.rectangle.setWidth(width);
    }

    @Override
    public ManagedTexture getManagedTexture() {
        return managedTexture;
    }

    public void setSharedTexturedMesh(TexturedMesh sharedTexturedMesh) {
        this.sharedTexturedMesh = sharedTexturedMesh;
    }

    @Override
    public TexturedMesh getTexturedMesh() {
        if (this.sharedTexturedMesh == null) {
            this.sharedTexturedMesh = new SharedTexturedMesh(this);
        }
        return this.sharedTexturedMesh;
    }

    @Override
    public IntRectangle getRectangle() {
        return rectangle;
    }

    @Override
    public String getTag() {
        return tag;
    }

    @Override
    public void setTag(String tag) {
        this.tag = tag;
    }
}
